<template>
    <div class="container containerExamine">
        <!-- header -->
        <div class="header">
            <p class="header-title">选择学校:</p>
            <el-cascader
                v-model="cityCollege"
                :options="dataList"
                @change="fnGetSchool"
                size="small"
                clearable
                :props="{
                    value: 'id',
                    label: 'name',
                    children: 'children'
                }"
            />
            <p class="header-title">班级:</p>
            <el-select
                v-model="classSchool"
                placeholder="请选择"
                size="small"
                clearable
                @change="getTableData(1)"
            >
                <el-option
                    v-for="item in classList"
                    :key="item.class_no"
                    :label="item.class_name"
                    :value="item.class_no"/>
            </el-select>
            <el-input
                v-model="phone"
                placeholder="请输入用户手机号"
                suffix-icon="el-icon-search"
                size="small"
                clearable
                class="search-ipt"
                @change="getTableData(1)"
                @keyup.native.enter="getTableData(1)"
            />
            <p class="header-title">状态:</p>
            <el-select
                v-model="status"
                placeholder="请选择"
                size="small"
                clearable
                @change="getTableData(1)"
            >
                <el-option
                    v-for="item in statusList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"/>
            </el-select>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
        >
            <el-table-column
                label="ID"
                width="100px"
                align="center"
                type="index"
            />
            <el-table-column
                prop="student.school"
                label="学校"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="student.true_name"
                label="用户姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="student.phone"
                label="用户手机号"
                show-overflow-tooltip
                :width="160"
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="class.school_college"
                label="学院名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="class.name"
                label="班级名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span
                        class="header-class"
                        @click="fnGoClass(scope.row.class)"
                    >{{ scope.row.class['name'] }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="certificate_image"
                label="班级群截图"
                width="130px"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <template v-if="scope.row.certificate_image !== '' ">
                        <a
                            href="javascript:;"
                            style="color: #409EFF"
                            @click="fnShowPreview(scope.row.certificate_image)">查看全部图片({{ 1 }})</a>
                    </template>
                    <template v-else>
                        --
                    </template>
                </template>
            </el-table-column>
            <el-table-column
                prop="monitor_name"
                label="班长姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="verify_fail_reason"
                label="备注"
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="verify"
                label="状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.verify == 1">待审核</span>
                    <span v-if="scope.row.verify == 2">审核不通过</span>
                    <span v-if="scope.row.verify == 3">审核通过</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                :width="250"
            >
                <template slot-scope="scope">
                    <span
                        v-if="scope.row.verify == 1 || scope.row.verify == 2"
                        class="curse-add"
                        @click="fnisOperation(scope.row, '3')"
                    >通过</span>
                    <span
                        v-if="scope.row.verify == 1 || scope.row.verify == 3"
                        @click="fnisOperation(scope.row, '2')"
                        class="curse-add">不通过</span>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- dialog -->
        <el-dialog
            title="*请认真填写,你的理由将推送给用户*"
            width="530px"
            custom-class="schedule-dialog"
            :visible.sync="showDilog"
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
            >
                <el-form-item
                    prop='text'
                >
                    <el-input
                        type="textarea"
                        :autosize="{ minRows: 2, maxRows: 6}"
                        placeholder="不通过理由"
                        v-model="ruleForm.text"
                    />
                </el-form-item>
            </el-form>
            <div
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    size="small"
                    type="primary"
                    @click="submitForm('ruleForm')">提交</el-button>
            </div>
        </el-dialog>

        <!-- 分页 -->
        <div
            class="pagination-wrapper clearfix"
            v-if="tableData.length > 0"
        >
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
        <!-- /分页 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
export default {
    components: {
        imgPreview
    },
    data () {
        return {
            tableData: [],
            dataList: [],
            cityCollege: '',
            classSchool: '',
            phone: '',
            status: '0',
            showDilog: false,
            statusList: [{
                value: '0',
                label: '全部'
            },{
                value: '1',
                label: '待审核'
            },{
                value: '2',
                label: '审核不通过'
            },{
                value: '3',
                label: '审核通过'
            }],
            classList: [{
                class_no: '',
                class_name: '全部'
            }],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            ruleForm: {
                text: ''
            },
            rules: {
                text: [
                    { required: true, message: '请填写不通过的理由', trigger: 'blur' }
                ]
            },
            id: '',
            verifyStatus: ''
        };
    },
    created () {
        this.setHeader();
        this.getTableData();
        this.fnGetCity();
    },
    watch: {
        showDilog (val) {
            if (!val) {
                this.ruleForm.text = '';
            }
        }
    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{
                title: "APP后台",
            },
            {
                title: '社交'
            },
            {
                title: "班级审核"
            }]
            );
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/forum_class_join_apply%5Cget_list', {
                page_id: page || this.page.now_page,
                school_id: this.cityCollege !== '' ? this.cityCollege[1] : '',
                college_no: this.cityCollege !== '' ? this.cityCollege[2] : '',
                class_no: this.classSchool,
                student_phone: this.phone,
                verify: this.status
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 获取三级联动数据
         */
        fnGetCity () {
            this.$post('/student/region_college%5Ccity_college_linkage', {
            }, resp => {
                if (resp.code == 1) {
                    this.dataList = resp.data;
                }
            });
        },

        /**
         * 获取班级列表
         */
        getClass () {
            this.$post('/student/region_class%5Cget_simple_list', {
                college_no: this.cityCollege !== '' ? this.cityCollege[2] : ''
            }, resp => {
                if (resp.code == 1) {
                    if (resp.data.length > 0) {
                        this.classList = this.classList.concat(resp.data);
                    }
                }
            });
        },

        /**
         * 选择学校
         */
        fnGetSchool () {
            this.getTableData(1);
            this.getClass();
        },

        /**
         * 查看图片
         */
        fnShowPreview(images) {
            let imgList = [];

            if (typeof(images) == 'object') {
                this.$refs.preview.show(images);
            } else if (typeof(images) == 'string') {
                imgList.push(images);
                this.$refs.preview.show(imgList);
            }
        },

        /**
         * 跳转班级管理
         */
        fnGoClass (item) {
            this.$router.replace({
                name: 'socialClass',
                query: {
                    name: item['name']
                }
            });
        },

        /**
         * 通过/不通过
         */
        fnisOperation (row, status) {
            this.id = row.id;
            this.verifyStatus = status;
            if (status == '3') {
                this.fnOperation('1');
            } else if (status == '2') {
                this.showDilog = !this.showDilog;
            }
        },

        submitForm (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.fnOperation('0');
                }
            });
        },

        fnOperation (status) {
            this.$post('/student/forum_class_join_apply%5Cverify', {
                id: this.id,
                verify: this.verifyStatus,
                verify_fail_reason: this.ruleForm.text
            }, resp => {
                if (resp.code == 1) {
                    if (status == '0') {
                        this.showDilog = !this.showDilog;
                    }
                    this.getTableData(1);
                    this.$message({
                        type: 'success',
                        message: '设置成功'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        widows: 100%;
        height: 100%;
        background-color: #fff;
        padding: 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        .header {
            margin-bottom: 20px;
            position: relative;
            display: flex;
            align-items: center;
            .header-title:first-child{
                margin-left: 0px;
            }
            .header-title {
                color: #606266;
                margin-right: 8px;
                margin-left: 20px;
            }
            .search-ipt {
                width: 200px;
                margin-left: 30px;
            }
            .header-right {
                display: flex;
                .el-button {
                    margin-left: 20px;
                }
            }
        }
        .curse-add {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .dialog-footer {
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
    .switch-fixed {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        right: 0;
        margin: auto;
        z-index: 999;
        cursor: pointer;
    }
    .header-class {
        color:rgb(64, 158, 255);
        cursor: pointer;
    }
</style>
<style lang="less">
    .containerExamine .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .containerExamine .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .containerExamine .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .containerExamine .el-dialog__title {
        font-size: 16px;
        color: #409EFF;
    }
</style>